package com.skywo.demo1.common.exception

import com.skywo.demo1.common.result.R
import com.skywo.demo1.common.result.RF
import com.skywo.demo1.common.result.ResultCode
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class GlobalExceptionHandler {

    private val log = LoggerFactory.getLogger(this::class.java)

    @ExceptionHandler(BusinessException::class)
    fun businessExceptionHandler(e: BusinessException): R<Nothing> {
        log.error("业务异常: {}", e.message)
        return RF.fail(e.code, e.message)
    }

    @ExceptionHandler(Exception::class)
    fun exceptionHandler(e: Exception): R<Nothing> {
        log.error("服务器异常: ", e)
        return RF.fail(ResultCode.SERVER_ERROR)
    }

}